﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.PlayerController
{
    [RequireComponent(typeof(Collider))]
    public class PlayerRadar : MonoBehaviour
    {
        private LibertyCarBirthManager _carBirthManager => LibertyAIUtils.AIManager.CarBirthManager;
        private HashSet<LibertyCarSeeker> _carSeekers;

        private LibertyNpcBirthManager _npcBirthManager => LibertyAIUtils.AIManager.NpcBirthManager;
        private HashSet<AIEntity> _aiEntities;

        private void Awake()
        {
            _carSeekers = new HashSet<LibertyCarSeeker>();
            for (int i = 0; i < 20; i++)
            {
                _carSeekers.Add(null);
            }
            _carSeekers.Clear();

            _aiEntities = new HashSet<AIEntity>();
            for (int i = 0; i < 20; i++)
            {
                _aiEntities.Add(null);
            }
            _aiEntities.Clear();
        }

        private void OnTriggerEnter(Collider other)
        {
            // _carBirthManager.col2CarSeekerMap.TryGetValue(other, out var carSeeker);
            // if (carSeeker != null)
            // {
            //     _carSeekers.Add(carSeeker);
            // }

            return;
            
            _npcBirthManager.col2AIEntityMap.TryGetValue(other, out var aiEntity);
            if (aiEntity != null)
            {
                _aiEntities.Add(aiEntity);
                aiEntity.OnEnterPlayerRadar();
            }
        }

        private void OnTriggerExit(Collider other)
        {
            // _carBirthManager.col2CarSeekerMap.TryGetValue(other, out var carSeeker);
            // if (carSeeker != null)
            // {
            //     if (_carSeekers.Contains(carSeeker))
            //     {
            //         _carSeekers.Remove(carSeeker);
            //         carSeeker.OnRemoveFromPlayerRadar();
            //     }
            // }
            
            return;
            
            _npcBirthManager.col2AIEntityMap.TryGetValue(other, out var aiEntity);
            if (aiEntity != null)
            {
                if (_aiEntities.Contains(aiEntity))
                {
                    _aiEntities.Remove(aiEntity);
                    aiEntity.OnExitPlayerRadar();
                }
            }
        }

        private void Update()
        {
            if (_carSeekers!=null)
            {
                var ge = _carSeekers.GetEnumerator();
                while (ge.MoveNext())
                {
                    if (ge.Current && ge.Current.transform)
                    {
                        ge.Current.OnPlayerRadarUpdate();
                    }
                }
                ge.Dispose();
            }

            return;
            
            if (_aiEntities!=null)
            {
                var ge = _aiEntities.GetEnumerator();
                while (ge.MoveNext())
                {
                    if (ge.Current && ge.Current.seeker &&
                        ge.Current.seeker.transform)
                    {
                        ge.Current.OnPlayerRadarUpdate();
                    }
                }
                ge.Dispose();
            }
            
        }
    }
}